<template>
    <!--最大div-->
    <div style="margin-top: -10px">
        <!--新建栏-->
        <div style="border:1px solid #BFBFBF;padding: 10px;background-color: white;margin-top: 10px;height: 40px">
            <el-row :gutter="24">
                <el-col :span="2">
                    <span style="font-size: 18px;line-height:40px;margin-left: 5px">产品规格</span>
                </el-col>
                <el-col :span="7">
                    <el-select  v-model="value7" @change="changebusni"
                                value-key="typeName"
                                style="width: 150px;margin-left: 20px"
                                placeholder="产品类型"
                    >
                        <el-option
                                v-for="item in optionsns"
                                :key="item.typeName"
                                :label="item.typeName"
                                :value="item">
                        </el-option>
                    </el-select>
                    <el-button style="background-color: #F05050;color: white;margin-left: 10px;" @click="cancelseek" icon="el-icon-back" v-show="seek" size="mini" round>解除搜索</el-button>
                </el-col>
                <el-col :span="9">
                    <el-input placeholder="请输入搜索的信息" v-model="input23" style="width: 250px;">
                        <i slot="suffix" class="el-input__icon el-icon-search" @click="initData"></i>
                    </el-input>
                </el-col>
                <el-col :span="6">
                    <el-button size="small"  type="primary" @click="dialogFormVisible = true">新建规格</el-button>
                    <el-dialog title="新增规格" width="38%" :visible.sync="dialogFormVisible" >
                        <el-form :model="form" >
                            <el-form-item v-model="form.modelId"  label="规格名：" :label-width="formLabelWidth">
                                <el-select v-model="form.modelId"
                                           value-key="modelId"
                                           placeholder="请选择"
                                           style="width: 232px;"
                                           filterable >
                                    <el-option v-for="item in options" :key="item.modelId" :label="item.typeId.typeName+item.modelName" :value="item"></el-option>
                                </el-select>

                                <!--新增规格名-->
                                <el-button style="margin-left: 30px" @click="dialogAormVisible = true">新建规格名</el-button>
                            </el-form-item>
                            <el-form-item label="规格值：" :label-width="formLabelWidth" style="width: 350px;">
                                <el-input v-model="form.modetContent" autocomplete="off"></el-input>
                            </el-form-item>
                        </el-form>
                        <div slot="footer" class="dialog-footer">
                            <el-button @click="quxiaoxinzen">取 消</el-button>
                            <el-button type="primary"  @click="xinzen">确 定</el-button>
                        </div>
                    </el-dialog>
                    <el-dialog title="新增规格名" :visible.sync="dialogAormVisible" width="30%" >
                        <el-form :model="formaa" :rules="rules">
                            <el-form-item label="产品类型：" :label-width="formLabelWidth"  prop="cplx">
                                <el-select v-model="formaa.typeId" value-key="typeId" placeholder="请选择" style="width: 232px;">
                                    <el-option v-for="item in optionsns" :key="item.typeId" :label="item.typeName" :value="item"></el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="属性名：" :label-width="formLabelWidth" prop="sxm"  style="width: 350px;">
                                <el-input v-model="formaa.modelName" autocomplete="off"></el-input>
                            </el-form-item>
                        </el-form>
                        <div slot="footer" class="dialog-footer">
                            <el-button @click="saveguigequxiao">取 消</el-button>
                            <el-button type="primary" @click="saveguige">确 定</el-button>
                        </div>
                    </el-dialog>
                </el-col>
            </el-row>
        </div>



        <!--表格数据框-->
        <div style="margin-top: 10px;border: 1px solid #BFBFBF;background-color: white">
            <div style="width: 100%;height: 40px;background-color: white;border-bottom: 1px solid #EBEEF5">
                <div class="operate" v-show="cjy">
                    <div class="selected—title" style="border-right: 0px ">
                        场景：<span>{{value7.typeName}}</span>
                    </div>
                </div>
                <div class="operate" v-show="cje">
                    <div class="selected—title" >
                        已选中<span class="selected—count">{{count}}</span>项
                    </div>
                    <div  class="selected—title1" v-show="isOne">
                        <span><i class="el-icon-edit"></i>修改</span>
                    </div>
                    <div  class="selected—title1" slot-scope="s">
                        <span @click="open2(s.row.modelId)"><i class="el-icon-delete"></i>删除</span>
                    </div>
                </div>
            </div>
            <el-table
                    :data="guigeList"
                    style="width: 100%"
                    max-height="450"
                    :default-sort = "{prop: 'date', order: 'descending'}"
                    @selection-change="handleSelectionChange">
                <el-table-column
                        type="selection"
                        width="50"
                        align="center">
                </el-table-column>
                <el-table-column
                        fixed
                        prop="modetId"
                        label="ID"
                        align="center"
                        width="100">
                </el-table-column>
                <el-table-column
                        fixed
                        prop="modelId.modelName"
                        label="属性名"
                        align="center">
                </el-table-column>
                <el-table-column
                        prop="modetContent"
                        label="属性值"
                        align="center">
                </el-table-column>
                <el-table-column
                        prop="modelId.typeId.typeName"
                        label="产品类型"
                        align="center"
                >
                </el-table-column>
                <el-table-column
                        fixed="right"
                        label="操作"
                        align="center"
                >
                    <template slot-scope="s">
                        <!--如果是三个标签，后两个标签padding-left: 10px;-->
                        <i style="font-size:18px;color: #409EFF" class="el-icon-edit"></i>
                        <i style="font-size:18px;padding-left: 20px;color: #409EFF" class="el-icon-delete" @click="open2(s.row.modelId)"></i>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <!--分页框-->
        <div style="margin-top: 15px;border: 1px solid #BFBFBF;padding: 10px;background-color: white">
            <div style="text-align: center">
                <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="pageNum"
                        :page-sizes="[2, 4, 6, 8]"
                        :page-size="pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="pageTotal"
                >
                </el-pagination>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        name: "Qchanpguige",
        data() {
            return {
                guigemin:'',
                drawer: false,
                multipleTable: [],//存放选中值的数组
                isOne:true,//选中项为1就出现添加跟进记录和修改
                dialogFormVisible: false,
                dialogAormVisible: false,
                //解除搜索是否显示
                seek:false,
                formLabelWidth: '120px',
                //下拉选择框
                options3: [{
                    options: [{
                        value: '成都成都',
                        label: '成都'
                    }, {
                        value: '深圳深圳',
                        label: '深圳'
                    }, {
                        value: '广州广州',
                        label: '广州'
                    }, {
                        value: '大连大连',
                        label: '大连'
                    }]
                }],
                rules: {
                    cplx: [
                        { required: true, message: '请选择类型名', trigger: 'blur' }
                    ],
                    sxm: [{ required: true, message: '请输入属性名', trigger:'blur'}]

                },
                value7: '',
                //场景一
                cjy:true,
                //场景二
                cje:false,
                //搜索框
                input23: '',
                //表格
                guigeList: [],
                //分页
                pageSize:4,//页大小
                pageTotal:0,//内容总条数
                pageNum: 1,//当前页
                formaa: {
                    modelId:0,
                    modelName:null,
                    typeId:{
                        typeId:0,
                        typeName:null
                    },
                },
                form: {
                    modelId:{
                        modelId:0,
                        modelName:null
                    },
                    modetId:0,
                    modetContent:null,

                },
                //规格名
                options:[],
                //类型名
                optionsns:[]
            }
        },

        //规格查询
        created:function() {
            this.initData(this.pageNum,this.pageSize);
            this.guige();
            this.guigee();
        },
        methods: {
            /*选择类型方法*/
            changebusni(){
                this.initData(this.pageNum,this.pageSize,this.input23,this.value7);

            },
            guige(){
                this.$axios.get("http://localhost:8088/par/niuniu-list").then(v=>{
                    this.options=v.data;
                }).catch();
            },
            guigee(){
                this.$axios.get("http://localhost:8088/par/gougou-list").then(v=>{
                    this.optionsns=v.data;
                }).catch();
            },
            //新增规格名
            saveguige(){
                var flag = 0;
                if(this.formaa.modelId==0){
                    this.options.forEach((atr)=>{
                        if(this.formaa.modelName == null ||  this.formaa.modelName == ""){
                            flag = 2;
                            return;
                        }
                        if(this.formaa.modelName==atr.modelName && this.formaa.typeId.typeName==atr.typeId.typeName){
                            flag = 1
                            return;
                        }
                    });
                }

                if(flag==2){
                    this.$message({
                        message: '请填写规格名！',
                        type: 'warning'
                    });
                }else if(flag==1){
                    this.$message({
                        message: '不能添加重复规格名！',
                        type: 'warning'
                    });
                }else if(flag==0){
                    this.dialogAormVisible = false;
                    this.$axios.post("http://localhost:8088/par/ggmxz-chanp",
                        this.formaa).then(()=>{
                        this.$message({
                            message: '新增成功',
                            type: 'success'
                        });
                        this.guige();
                        this.formaa.typeId='';
                        this.formaa.modelName='';
                    }).catch();
                }
            },

            /*取消新建规格名*/
            saveguigequxiao(){
                this.dialogAormVisible = false;
                this.formaa.typeId='';
                this.formaa.modelName='';
            },

            quxiaoxinzen(){
                this.dialogFormVisible = false;
                this.form.modelId='';
                this.form.modetContent=''

            },
            /*新建规格方法*/
            xinzen(){
                var flag = 0;
                if(this.form.modetId==0){
                    this.guigeList.forEach((atr)=>{
                        if(this.form.modetContent =='' || this.form.modetContent ==null){
                            flag=2;
                            return;
                        }
                        if(this.form.modelId.typeId.typeName==atr.modelId.typeId.typeName
                            && this.form.modelId.modelName==atr.modelId.modelName
                            && this.form.modetContent==atr.modetContent){
                            flag = 1;
                            return;

                        }
                    });
                }
                if(flag==2){
                    this.$message({
                        message: '请填写规格值！',
                        type: 'warning'
                    });
                }else if(flag==1){
                    this.$message({
                        message: '不能添加重复规格哦！',
                        type: 'warning'
                    });
                }else{
                    this.dialogFormVisible = false;
                    this.$axios.post("http://localhost:8088/par/ggzxz-chanp",
                        this.form).then(()=>{
                        this.$notify({
                            title:'成功',
                            message: '新增成功！',
                            type: 'success'
                        });
                        this.initData(this.pageNum,this.pageSize);
                        this.form.modelId='';
                        this.form.modetContent=''
                    }).catch();
                }
            },
            /*跳转详情方法*/
            todetails(rows){
                this.$router.push({
                    path:'/business/details',
                    query: {
                        row:rows
                    }

                })
            },

            handleSizeChange(val) {
                this.pageSize = val;
                this.initData();
            },
            //翻页数 改变信息
            handleCurrentChange(v){
                this.pageNum = v;
                this.initData();
            },
            //分页
            initData(){

                this.$axios.post("http://localhost:8088/par/guige-list",this.qs.stringify({
                    pageNum:this.pageNum,PageSize:this.pageSize,input23:this.input23,value7:this.value7.typeId}))
                    .then(v=>{
                        this.guigeList = v.data.rows;
                        this.pageTotal = v.data.total;
                        window.console.log(v.data.rows);
                    }).catch()
            },

            /*表格删除方法*/
            open2(idd){
                this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$axios.post("http://localhost:8088/par/delete-cpgg",this.qs.stringify(
                        {id:idd}))
                        .then(()=>{
                            this.initData();
                            this.$notify({
                                title:'成功',
                                type: 'success',
                                message: '删除成功!'
                            });
                        }).catch(v=>{
                        alert("加载失败删除："+v.data);
                    })
                }).catch(() => {
                    this.$notify({
                        title:'取消',
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            //复选框选中的
            handleSelectionChange(val) {
                this.multipleTable = val;//  this.multipleTable 选中的值
                if(this.multipleTable.length>0){
                    this.cjy=false;//第一行隐藏
                    this.cje=true;//批量操作栏出现
                }
                //选中项大于1不能执行添加跟进记录和修改按钮
                if(this.multipleTable.length>1){
                    this.isOne=false;
                }else if(this.multipleTable.length==0){//选中项为0时
                    this.cjy=true;//第一行出现
                    this.cje=false;//批量操作栏隐藏
                }else{
                    this.isOne=true;//添加跟进记录和修改出现
                }
                window.console.log(val);
            },
            /*解除选择类型商机方法*/
            cancelseek(){
                this.value7='';
                this.seek=false;
            }
        },
        computed:{
            count(){
                return this.multipleTable.length
            },
        },


    }
</script>

<style scoped>

    .addview:hover{
        color: white;
    }
    a{
        text-decoration: none;
        color: #409EFF;
    }
    a:hover{
        color: white;
    }
    .busin:hover{
        color: #3E84E9;
        cursor:pointer;
    }
    .businame{
        color: #606266;
    }
    .businame:hover{
        color: #409EFF;
        cursor:pointer;
    }
    .operate{
        font-size: 12px;
        height: 40px;
        padding: 0 20px;
        color: #777;
        background: white;
    }
    .selected—title{
        margin-top: 15px;
        margin-left: -6px;
        border-right: 1px solid #e6e6e6;
        width: 86px;
        height:20px;
        float: left;
    }
    .selected—title1{
        margin-top: 15px;
        border-right: 1px solid #e6e6e6;
        width: 86px;
        height:20px;
        float: left;
        cursor: pointer;
    }

    .selected—title1 span{
        margin-left: 24px;
    }
    .selected—title1 span:hover{
        color:#409eff;
    }
    .selected—title2{
        margin-top: 15px;
        border-right: 1px solid #e6e6e6;
        width: 129px;
        height:20px;
        float: left;
        cursor: pointer;
    }

    .selected—title2 span{
        margin-left: 24px;
    }
    .selected—title2 span:hover{
        color:#409eff;
    }
</style>